﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleScale.DataAccess;
using VehicleScale.Models;
using VehicleScale.Reports;

namespace VehicleScale.Forms.Vehicles
{
    public partial class FrmVehicleScaleList : TemplateForm
    {

        #region Ctors
        private FrmVehicleScaleList()
        {
            InitializeComponent();

            Initialize();

            this.panel1.BackColor = Setting.BackColor;
            this.uiGroupBox1.BackColor = Setting.BackColor;
            this.Size = new Common().SizeList;
        }
        #endregion

        #region Singleton Members
        private static volatile FrmVehicleScaleList instance;
        private static object syncRoot = new Object();

        public static FrmVehicleScaleList Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FrmVehicleScaleList();
                }

                return instance;
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            base.OnClosed(e);
        }
        #endregion

        #region Field Members
        IVehicleScaleDataAccess _dataAccess = new VehicleScaleDataAccess();
        IList<VehicleScaleModel> _vehicleScaleList;
        #endregion

        #region Method Members

        /// <summary>
        /// Initialize methods of the application
        /// </summary>
        void Initialize()
        {
            LoadCustomersToCombo();
            LoadProductsToCombo();
            LoadCategorysToCombo();
            this.btnReset.Click += new EventHandler(delegate { ClickButtonReset(); });
            this.btnSearch.Click += new EventHandler(delegate { ClickButtonSearch(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnExcel.Click += new EventHandler(delegate { base.OnExportExcel(this.gridEX1); });
            this.btnDelete.Click += new EventHandler(delegate { ClickButtonDelete(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
            this.gridEX1.FormattingRow += new Janus.Windows.GridEX.RowLoadEventHandler(gridEX1_FormattingRow);

            // Default value
            this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = String.Empty;

            // Show all customers
            this.ClickButtonSearch();

            if (Common.UserLogin.GroupPermissionID != 1)
            {
                this.btnDelete.Visible = false;
            }
          //  LogFile.WriteLog(LogFile.nameFile,LogFile.userLogin,"Mo Form Xem danh sach phieu can");
        }

        void gridEX1_FormattingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
        {
            if (e.Row.Cells["ImExType"].Value == null) return;
            if (e.Row.Cells["ImExType"].Value.Equals(1))
            {
                e.Row.Cells["ImExType"].Text = "Nhập hàng";
            }
            else
            {
                e.Row.Cells["ImExType"].Text = "Xuất hàng";
            }

        }

        #endregion

        #region Action Members

        /// <summary>
        /// Click button reset
        /// </summary>
        private void ClickButtonReset()
        {
            this.cboCustomerID.SelectedIndex = -1;
            this.cboProductID.SelectedIndex = -1;
            this.cboCustomerID.SelectedIndex = -1;
            this.calendarFromDate.Value = DateTime.Today;
            this.calendarToDate.Value = DateTime.Today;

            this.ClickButtonSearch();

            this.cboCustomerID.Focus();
        }

        /// <summary>
        /// Click button search
        /// </summary>
        private void ClickButtonSearch()
        {
            _dataAccess.SearchObject = new VehicleScaleModel();
            if (this.cboCustomerID.Value != null)
                _dataAccess.SearchObject.CustomerID = this.cboCustomerID.Value.ToString();
            if (this.cboProductID.Value != null)
                _dataAccess.SearchObject.ProductID = this.cboProductID.Value.ToString();
            if (this.cboCategoryID.Value != null)
                _dataAccess.SearchObject.CategoryID = this.cboCategoryID.Value.ToString();

            _dataAccess.SearchObject.ScaleDate = this.calendarFromDate.Value;
            _dataAccess.ToDate = this.calendarToDate.Value;
            if (rdoTypeAll.Checked)
            {
                _dataAccess.SearchObject.ImExType = -1;
            }
            else if (rdoTypeImport.Checked)
            {
                _dataAccess.SearchObject.ImExType = 1;
            }
            else if (rdoTypeExport.Checked)
            {
                _dataAccess.SearchObject.ImExType = 2;
            }

            //if (Common.UserLogin != null && Common.UserLogin.GroupPermissionID == 0)
            //{
            //    _dataAccess.SearchObject.EmployeeID = Common.UserLogin.EmployeeID;
            //}

            _vehicleScaleList = _dataAccess.Searcher();

            if (_vehicleScaleList == null || _vehicleScaleList.Count == 0)
                this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Không tìm thấy thông tin."; // Bạn phải nhập ít nhất 1 tiêu chí tìm kiếm.";
            else
                this.gridEX1.BuiltInTexts[Janus.Windows.GridEX.GridEXBuiltInText.GroupByBoxInfo] = "Chọn cột cần group.";

            this.gridEX1.DataSource = _vehicleScaleList;
        }

        /// <summary>
        /// Click button edit
        /// </summary>
        private void ClickButtonEdit()
        {
            if (this.gridEX1.CurrentRow == null)
            {
                Library.Tools.MsgBox.Show(Messages.EditMessage, Messages.NoticeTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                LogFile.WriteLog(LogFile.nameFile, LogFile.userLogin, "Click Dieu chinh Phieu can xe");
            }
            catch
            {
            }
            if (FrmMDI.Instance.btnBarCustomerList.Groups[0].Items.Count > 0)
            {
                DialogResult result1 = Library.Tools.MsgBox.Show("Khi chỉnh sửa phiếu bạn vui lòng đóng hết tất cả các phiếu đang cân!\nBạn có chấp nhận đóng hết tất cả phiếu đang cân không?", "Thông báo",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (result1 == DialogResult.Yes)
                {
                    foreach (var item in FrmMDI.Instance.MdiChildren)
                    {
                        item.Close();
                    }
                }
                else if (result1 == DialogResult.Cancel)
                {
                    return;
                }
            }

            // Edit employee information
            FrmVehicleScale form = new FrmVehicleScale(this.gridEX1.CurrentRow.DataRow as VehicleScaleModel);
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridEX1.Refetch();
            }
        }

        /// <summary>
        /// Click button delete
        /// </summary>
        private void ClickButtonDelete()
        {
            if (this.gridEX1.CurrentRow == null)
            {
                Library.Tools.MsgBox.Show(Messages.DeleteMessage, Messages.NoticeTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            VehicleScaleModel vehicleScale = this.gridEX1.CurrentRow.DataRow as VehicleScaleModel;
            DialogResult result = Library.Tools.MsgBox.Show(Messages.ConfirmDeleteMessage, Messages.QuestionTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK)
            {
                if (_dataAccess.Delete(vehicleScale))
                {
                    this.gridEX1.CurrentRow.Delete();
                    try
                    {
                        LogFile.WriteLog(LogFile.nameFile, LogFile.userLogin, "Xoa phieu can : " + vehicleScale.VehicleScaleID);
                    }
                    catch
                    {
                    }
                }
                else
                {
                    Library.Tools.MsgBox.Show(String.Format(Messages.DeleteFailedMessage, "phiếu cân"),
                        "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        #endregion

        #region Load data and device
        /// <summary>
        /// Load customer list to combo
        /// </summary>
        private void LoadCustomersToCombo()
        {
            this.cboCustomerID.DataSource = Common.Customers;
            this.cboCustomerID.ValueMember = "CustomerID";
            this.cboCustomerID.DisplayMember = "CustomerName";
        }

        /// <summary>
        /// Load product list to combo
        /// </summary>
        private void LoadProductsToCombo()
        {
            this.cboProductID.DataSource = Common.Products;
            this.cboProductID.ValueMember = "ProductID";
            this.cboProductID.DisplayMember = "ProductName";
        }

        //Load Catrgory

        private void LoadCategorysToCombo()
        {
            this.cboCategoryID.DataSource = Common.Categories;
            this.cboCategoryID.ValueMember = "CategoryID";
            this.cboCategoryID.DisplayMember = "CategoryName";
        }
        #endregion

       

     

    }
}
